package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

/**
 * 客运公司车型信息Controller
 * @author wuhao
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentproduct/tCargotype")
public class TCargotypeController extends BaseController {

	@Autowired
	private TCargotypeService tCargotypeService;
	
	@ModelAttribute
	public TCargotype get(@RequestParam(required=false) String cargotypeid) {
		TCargotype entity = null;
		if (StringUtils.isNotBlank(cargotypeid)){
			entity = tCargotypeService.get(cargotypeid);
		}
		if (entity == null){
			entity = new TCargotype();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tCargotype:view")
	@RequestMapping(value = {"list", ""})
	public String list(TCargotype tCargotype, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TCargotype> page = tCargotypeService.findPage(new Page<TCargotype>(request, response), tCargotype); 
		model.addAttribute("page", page);
		model.addAttribute("tCargotype", tCargotype);
		return "modules/rentproduct/tCargotypeList";
	}

	@RequiresPermissions("rentproduct:tCargotype:view")
	@RequestMapping(value = "form")
	public String form(TCargotype tCargotype, Model model) {
		if(ValidateTool.isEmptyOrNull(tCargotype.getIselectric())){
			tCargotype.setIselectric("0");
		}
		model.addAttribute("tCargotype", tCargotype);
		return "modules/rentproduct/tCargotypeForm";
	}

	@RequiresPermissions("rentproduct:tCargotype:edit")
	@RequestMapping(value = "save")
	public String save(TCargotype tCargotype, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, tCargotype)){
			return form(tCargotype, model);
		}
		tCargotypeService.save(tCargotype);
		addMessage(redirectAttributes, "保存客运公司车型信息成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCargotype/?repage";
	}
	
	@RequiresPermissions("rentproduct:tCargotype:edit")
	@RequestMapping(value = "delete")
	public String delete(TCargotype tCargotype, RedirectAttributes redirectAttributes) {
		tCargotypeService.delete(tCargotype);
		addMessage(redirectAttributes, "删除客运公司车型信息成功");
		return "redirect:"+Global.getAdminPath()+"/rentproduct/tCargotype/?repage";
	}

	/**
	 * 验证车型名称是否唯一
	 * @param oldTypename
	 * @param typename
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("rentproduct:tCargotype:edit")
	@RequestMapping(value = "checkTypename")
	public String checkTypename(String typename,String oldTypename) {
		if (typename != null && typename.equals(oldTypename)) {
			return "true";
		} else if (typename != null && tCargotypeService.getCartypeByTypename(typename) == null){
			return "true";
		}
		return "false";
	}
	
}